package cn.tedu;

import java.lang.reflect.Field;
import java.sql.*;
import java.util.ArrayList;

/*本类主要用于对Jdbc进行封装操作*/
public class Jdbc {
    /**
     * 本方法用于获取数据库连接
     * @return
     */
    private Connection getConnection(){
        Connection connection=null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
             connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/cgb2105");
        }catch (Exception e){
            System.out.println("连接异常");
        }
        return connection;
    }

    /**
     * 通用的查询操作
     * @param clazz
     * @param sql
     * @param obj
     * @param <T>
     * @return
     */
    public<T>ArrayList select(Class<T> clazz,String sql,Object...obj){
        Connection connection =null;
        PreparedStatement preparedStatement =null;
        ResultSet resultSet=null;
        ArrayList<Object> objects = new ArrayList<>();
        try {


             connection = getConnection();
             preparedStatement = connection.prepareStatement(sql);
            for (int i = 0; i < obj.length; i++) {
                preparedStatement.setObject(i + 1, obj[i]);
            }
             resultSet = preparedStatement.executeQuery();
            ResultSetMetaData metaData = resultSet.getMetaData();
            int columnCount = metaData.getColumnCount();
            while (resultSet.next()) {
                T t = clazz.newInstance();
                for (int i = 0; i < columnCount; i++) {
                    String cursorName = resultSet.getCursorName();
                    Object object = resultSet.getObject(i);
                    Field declaredField = clazz.getDeclaredField(cursorName);
                    declaredField.setAccessible(true);
                    declaredField.set(t, object);
                }
                objects.add(t);
            }
        }catch (Exception e){
            System.out.println("查询异常");
        }finally {
            close(resultSet,preparedStatement,connection);
        }
        return objects;
    }
    public void update(String sql,Object...obj){
        Connection connection =null;
        PreparedStatement preparedStatement =null;
        try {
             connection = getConnection();
             preparedStatement = connection.prepareStatement(sql);
            for (int i = 0; i < obj.length; i++) {
                preparedStatement.setObject(i + 1, obj[i]);
            }
            preparedStatement.executeUpdate();
        }catch (Exception e){
            System.out.println("更新失败");
        }finally {
            close(preparedStatement,connection);
        }
    }
    private void close(Object...obj){
        try {
            for (int i = 0; i < obj.length; i++) {
                if (obj[i] instanceof PreparedStatement&&obj[i]!=null) {
                    ((PreparedStatement)
                        obj[i]).close();
                }
                if (obj[i] instanceof Connection&&obj[i]!=null) {
                    ((Connection) obj[i]).close();
                }
                if (obj[i] instanceof ResultSet&&obj[i]!=null) {
                    ((ResultSet) obj[i]).close();
                }
            }
        }catch (Exception e){
            System.out.println("资源关闭异常");
        }
    }
}
